package io;

import java.io.*;

public class dicectory {
	public static void main(String[] args) {
		try{
			//an object of type File is used to represent either a file or a directory
			File file1 = new File("foo1");	//no file yet
			file1.createNewFile();	//make a file named "foo1" which is assigned to 'file1'
			
			File file2 = new File("foo2");	//no file yet
			PrintWriter pw1 = new PrintWriter(file2);	//make a PrinterWriter object AND
														//make a file named "foo2" to which 'file' is assigned AND
														//assign 'pw1' to the PrintWriter
			
			File myDir1 = new File("myDir1");	//create an object
			myDir1.mkdir();	//create an actual directory
			
			File myFile1 = new File(myDir1, "myFile1.txt");
			myFile1.createNewFile();
			
			PrintWriter pw2 = new PrintWriter(myFile1);
			pw2.println("new stuff");
			pw2.flush();
			pw2.close();
			
			//refer a File object to an existing file or directory,
			//assuming that we already have a sub-directory called mydir in which resides an existing file myFile1.txt
			File existingDir = new File("myDir1");	//assign a dir
			System.out.println(existingDir.isDirectory());
			
			File existingDirFile = new File(existingDir, "myFile1.txt");	//assign a file
			System.out.println(existingDirFile.isFile());
			
			FileReader fr1 = new FileReader(existingDirFile);
			BufferedReader br1 = new BufferedReader(fr1);	//make a reader
			
			String s;
			while( (s = br1.readLine()) != null )
				System.out.println(s);
			
			br1.close();
			
			//rename or delete files
			File delDir = new File("delDir");	//make a directory
			delDir.mkdir();
			
			File delFile1 = new File(delDir, "delFile1.txt");	//add file to a directory
			delFile1.createNewFile();
			
			File delFile2 = new File(delDir, "delFile2.txt");	//add file to a directory
			delFile2.createNewFile();
			
			PrintWriter pw3 = new PrintWriter(delFile1);
			pw3.println("file to be deleted");
			pw3.flush();
			pw3.close();
			//delFile1.delete();	//delete a file
			//delFile2.delete();
			System.out.println("delDir is " + delDir.delete());	//attempt to delete the directory
			
			File newName = new File(delDir, "newName.txt");	//a new object
			delFile2.renameTo(newName);	//rename file
			
			File newDir = new File("newDir");	//rename directory
			delDir.renameTo(newDir);
				
		} catch(IOException e) {e.printStackTrace();}
	}
}
